<template>
	<view class="setting">
		<view class="information">
			<view class="title ml-20">基本信息</view>
			<u-field v-model="dataForm.name" required label="名称" input-align='right' placeholder="请输入名称"></u-field>
			<u-field v-model="dataForm.phone" required label="电话" input-align='right' placeholder="请输入电话"></u-field>
			<u-field v-model="dataForm.address" label="地址" input-align='right' placeholder="请输入地址"></u-field>
			<u-field v-model="dataForm.remark" type='textarea' label="备注" input-align='right' placeholder="请输入备注">
			</u-field>
		</view>

		<view class="information">
			<view class="title ml-20">修改密码</view>
			<u-field v-model="passForm.oldpassword" password label="旧密码" input-align='right' placeholder="请输入旧密码">
			</u-field>
			<u-field v-model="passForm.newpassword" password label="新密码" input-align='right' placeholder="请输入新密码">
			</u-field>
			<u-field v-model="passForm.againnew" password label="再次输入" input-align='right' placeholder="请再输入"></u-field>
		</view>
		<view class="save" @click="submit">保存</view>
	</view>
</template>

<script>
	import {
		GetQueryDataList,
		adminPost,
		UpdateModifyData
	} from '@/common/http.request.js'
	export default {
		data() {
			return {
				dataForm: {
					name: '',
					phone: '',
					remark: '',
					address: ''
				},
				passForm: {
					oldpassword: '',
					newpassword: '',
					againnew: ''
				},
			}
		},
		onLoad() {
			GetQueryDataList('b_enroll_mechanism', 1, 1, {
				pk_id: this.$store.state.vuex_adminuser.enroll_id
			}, {
				setting: true
			}).then(res => {
				this.dataForm = res.data[0]
			})
		},
		methods: {
			async submit() {
				let newpw =this.passForm.newpassword? (await this.$adminPost('GetMD5Encryption', {
					password: this.passForm.newpassword
				})).data:''
				let oldpassword = this.passForm.oldpassword?(await this.$adminPost('GetMD5Encryption', {
					password: this.passForm.oldpassword
				})).data:''
				if (oldpassword != this.dataForm.password) {
					return this.$toast('旧密码不正确') 
				} else if (oldpassword && (!newpw || !this.passForm.againnew)) {
					return this.$toast('密码信息不能为空')
				} else if (this.passForm.newpassword.length < 6 || this.passForm.againnew.length < 6 ) {
					return this.$toast('密码不能少于6位')
				} else if ( this.passForm.newpassword != this.passForm.againnew){
						return this.$toast('两次密码不一致')
				}else if (newpw == this.dataForm.password) {
					return this.$toast('新密码不能与旧密码相同')
				} else if (!this.dataForm.name || this.dataForm.name.trim() == '') {
					return this.$toast('联系人不能为空')
				} else if (!this.dataForm.phone || this.dataForm.phone.trim() == '') {
					return this.$toast('手机号不能为空')
				}else if (newpw != this.dataForm.password){
						this.dataForm.password = newpw
				}
				this.$UpdateModify('b_enroll_mechanism',this.dataForm).then(res=>{
					if (this.passForm.oldpassword && this.passForm.newpassword) {
						this.$u.vuex('vuex_adminuser', {});
						this.$linkTo('/package/system/login')
						return
					}
					this.$u.vuex('vuex_adminuser', {
						...this.$store.state.vuex_adminuser,
						...this.dataForm
					});
					this.uniBack(1)
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: #f5f5f5;
	}
</style>

<style lang='scss' scoped>
	.setting {
		padding: 0 30rpx;

		.ml-20 {
			margin-left: 20rpx;
		}

		.information {
			width: 100%;
			background-color: #fff;
			border-radius: 20rpx;
			margin: 30rpx 0;
			padding: 20rpx;

			.title {
				font-weight: bold;
				font-size: 32rpx;
			}
		}

		.box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;

			.box-tit {}

			.box-swit {
				display: flex;
				align-items: center;
			}
		}

		.save {
			width: 100%;
			line-height: 80rpx;
			text-align: center;
			background-color: #49a3f1;
			color: #fff;
			border-radius: 20rpx;
			margin-bottom: 40rpx;
		}
	}
</style>
